package com.example.demo.constants;

import java.io.Serializable;

import org.apache.commons.collections.functors.TruePredicate;

import com.fasterxml.jackson.annotation.JsonInclude;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * 返回数据实体
 * @author Administrator
 *
 * @param <T>
 */
@ApiModel
@Api(tags = "返回实体")
@JsonInclude(JsonInclude.Include.NON_NULL)
public final class ResultEnity<T> implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	// 成功状态
    @ApiModelProperty(value = "成功状态，成功为true 失败为false", name = "成功状态")
    private boolean success;

	// 200成功
    @ApiModelProperty(value = "状态码 成功为 200 授权未验证 401 服务器错误 500", name = "状态码")
    private int code;
 
    // 返回消息，成功为“success”，失败为具体失败信息
    @ApiModelProperty(value = "提示信息，如果有错误则是错误描述", name = "错误码描述")
    private String message;
 
    // 返回数据
    @ApiModelProperty(value = "数据对象", name = "数据对象")
    private T data;
    
    // 返回数据
    @ApiModelProperty(value = "堆栈信息", name = "堆栈信息", hidden = true )
    private Throwable exThrowable;
 
    public ResultEnity() {
    }
 
    public ResultEnity(int code, Boolean success, String message) {
        this.code = code;
        this.success=success;
        this.message = message;
        this.exThrowable=null;
    }
 
    public ResultEnity(int code, Boolean success, String message, T data) {
        this.code = code;
        this.success=success;
        this.message = message;
        this.data = data;
        this.exThrowable=null;
    }  
    
    public ResultEnity(int code, Boolean success, String message, Throwable ex) {
        this.code = code;
        this.success=success;
        this.message = message;
        this.data = null;
        this.exThrowable=ex;
    }  

    public static <T extends Object> ResultEnity<T> Success(String message, T data){
    	ResultEnity<T> result = new ResultEnity<T>(200, true, message, (T)data);  	
    	return result;
    }
    
    public static <T extends Object> ResultEnity<T> Error(String message,Throwable ex) {
    	ResultEnity<T> result = new ResultEnity<T>(500, false, message, ex);   	
    	return result;
    }
    
    public boolean getSuccess() {
    	return this.success;
    }
    
    public void setSuccess(boolean success) {
    	this.success=success;
    }
 
    public int getCode() {
        return code;
    }
 
    public void setCode(int code) {
        this.code = code;
    }
 
    public String getMessage() {
        return message;
    }
 
    public void setMessage(String message) {
        this.message = message;
    }
 
    public T getData() {
        return data;
    }
 
    public void setData(T data) {
        this.data = data;
    }
 
    @Override
    public String toString() {
        return "{" +               
                "code=" + code +
                ", success="+success+
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }

}
